package com.lxh.iot.controller;

import com.lxh.common.core.web.domain.AjaxResult;
import com.lxh.iot.service.ISocialLoginService;
import me.zhyd.oauth.model.AuthCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController
@RequestMapping("/auth")
public class SocialLoginController {

    @Autowired
    private ISocialLoginService iSocialLoginService;


    @GetMapping("/render/{source}")
     public void renderAuth(HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest, @PathVariable String source) throws IOException {
        // 生成授权页面
        httpServletResponse.sendRedirect(iSocialLoginService.renderAuth(source, httpServletRequest));
    }

    @GetMapping("/callback/{source}")
    public void login(@PathVariable("source") String source, AuthCallback authCallback, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws IOException {
        //回调接口
        httpServletResponse.sendRedirect(iSocialLoginService.callback(source, authCallback, httpServletRequest));
    }

    @GetMapping("/checkBindId/{bindId}")
    public AjaxResult checkBindId(HttpServletResponse httpServletResponse, @PathVariable String bindId) {
        return iSocialLoginService.checkBindId(bindId);
    }

    @GetMapping("/getErrorMsg/{errorId}")
    public AjaxResult getErrorMsg(HttpServletResponse httpServletResponse, @PathVariable String errorId) {
        return iSocialLoginService.getErrorMsg(errorId);
    }

    /**
     * 已经绑定账户，跳转登录接口
     *
     * @param loginId
     * @return
     */
    @GetMapping("/login/{loginId}")

     public AjaxResult socialLogin(@PathVariable String loginId) {
        // 生成授权页面
        return iSocialLoginService.socialLogin(loginId);
    }

//    /**
//     * 登录方法
//     *
//     * @param bindLoginBody 绑定登录信息
//     * @return 结果
//     */
//    @PostMapping("/bind/login")
//    public AjaxResult bindLogin(@RequestBody BindLoginBody bindLoginBody) {
//        return iSocialLoginService.bindLogin(bindLoginBody);
//    }
//
//    /**
//     * 注册绑定接口
//     *
//     * @param bindRegisterBody 注册信息
//     * @return
//     */
//    @PostMapping("/bind/register")
//    public AjaxResult bindRegister(@RequestBody BindRegisterBody bindRegisterBody) {
//        return iSocialLoginService.bindRegister(bindRegisterBody);
//    }


}



